package com.ez.supervise.action.sys;

import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.dto.UserCheckRole;
import com.ez.supervise.model.sys.UserRole;
import com.ez.supervise.service.RoleService;
import com.ez.supervise.service.UserRoleService;
import com.ez.supervise.service.UserService;
import com.ez.supervise.util.JsonUtils;


public class UserRoleAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long[] ids;
    private UserRole userRole;
    private Page page;
    private Long userId;

    private UserRoleService userRoleService;
    private UserService userService;
    private RoleService roleService;

    public String saveUserRole() {
	try {
	    userRoleService.saveRolePermissionByRoleId(userId, ids);
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    e.printStackTrace();
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllUserRole() {
	page = new Page();
	try {
	    List roles = roleService.findAllRole();
	    List userRoles = userRoleService.findUserRoleByUserId(userId);
	    List result = UserCheckRole.build(roles, userRoles);
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });
	    JSONObject jsonObj = JSONObject.fromObject(page, config);
	    System.out.println(jsonObj.toString());
	    outJsonString(jsonObj.toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllUser() throws Exception {
	page = new Page();
	page.setRoot(userService.findAllUser());
	page.setTotalProperty(page.getRoot().size());
	outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
	    "isDeleted", "new" }));

	return SUCCESS;

    }

    /**
     * @return
     */
    public String deleteUserRole() {
	try {
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateUserRole() throws Exception {
	try {
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public static void main(String[] args) {
	UserRole userRole = new UserRole();
	userRole.setId(1L);
	// userRole.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(userRole);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime" });

	JSONObject jsonObj = JSONObject.fromObject(userRole, config);
	System.out.println(jsonObj.toString());

    }

    public void setUserRoleService(UserRoleService userRoleService) {
	this.userRoleService = userRoleService;
    }

    public UserRoleService getUserRoleService() {
	return userRoleService;
    }

    public void setUserRole(UserRole userRole) {
	this.userRole = userRole;
    }

    public UserRole getUserRole() {
	return userRole;
    }

    public void setIds(Long[] ids) {
	this.ids = ids;
    }

    public void setUserId(Long userId) {
	this.userId = userId;
    }

    public void setUserService(UserService userService) {
	this.userService = userService;
    }

    public void setRoleService(RoleService roleService) {
	this.roleService = roleService;
    }
}
